

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractBaiduQueries {
	static Pattern BaiduQuerySig = Pattern.compile("(?<=(wd=|word=).)");
	static Pattern BaiduQuery = Pattern.compile("(?<=(wd=)|(word=)|(bs=))[^&\" ]+");
	
	public static void ExtractWorker(String logloc,String outputloc) throws IOException {
			RandomAccessFile raf = new RandomAccessFile(logloc,"r");
			JSave saver = new JSave();
			
			String logEntry = null;
			
			while ((logEntry = raf.readLine()) != null) {
				Matcher baiduQueryMatcher = BaiduQuerySig.matcher(logEntry);
				boolean x = baiduQueryMatcher.find();
				if (x) {
					String[] logEntryFields = logEntry.split(" ", 11);
					
					Matcher mBaiduQuery = BaiduQuery.matcher(logEntryFields[10]);
					boolean y = mBaiduQuery.find();
					
					if (y) {
						System.out.println(mBaiduQuery.group(0));
						saver.Add(outputloc, mBaiduQuery.group(0));
					}
				}
			}
			raf.close();
	}
	
	public static void main (String args[]) throws IOException {
		Decoder d = new Decoder();
		String extractIn = args[0];
		String extractOut = args[1];
		ExtractWorker(extractIn,extractOut);
		d.Decode(extractOut, extractOut.concat(".decoded"));
	}
}


